package cool.mtc.minghe.service.manager.sys.dict.impl;

import cool.mtc.minghe.data.model.base.Dict;
import cool.mtc.minghe.service.manager.BaseManagerImpl;
import cool.mtc.minghe.service.manager.sys.dict.DictManager;
import cool.mtc.minghe.service.mapper.sys.dict.DictMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 明河
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DictManagerImpl extends BaseManagerImpl implements DictManager {
    private final DictMapper dictMapper;

    @Override
    @Cacheable(value = "dict_map:dict", key = "'all'", unless = "null == #result")
    public Map<String, String> mapDict() {
        List<Dict> list = dictMapper.listAll();
        Map<String, String> map = new HashMap<>();
        list.forEach(item -> map.put(item.getKey(), item.getValue()));
        return map;
    }

    @Override
    @Cacheable(value = "dict:dict", key = "'dict_type_code_' + #dictTypeCode", unless = "null == #result")
    public List<Dict> listDictByDictTypeCode(String dictTypeCode) {
        return dictMapper.listDictByDictTypeCode(dictTypeCode);
    }

    @Override
    @CacheEvict(value = {"dict:dict", "dict_map:dict"}, key = "'dict_type_code_' + #dictTypeCode", allEntries = true)
    public void clearDictCacheByDictTypeCode(String dictTypeCode) {
    }

    @Override
    @Cacheable(value = "dict:dict", key = "'dict_type_id_' + #dictTypeId", unless = "null == #result")
    public List<Dict> listDictByDictTypeId(long dictTypeId) {
        return dictMapper.listDictByDictTypeId(dictTypeId);
    }

    @Override
    @CacheEvict(value = {"dict:dict", "dict_map:dict"}, key = "'dict_type_id_' + #dictTypeId", allEntries = true)
    public void clearDictCacheByDictTypeId(long dictTypeId) {
    }
}
